package com.ziyueyuwen.xiniao.service.impl;

import com.ziyueyuwen.xiniao.bean.ZyRole;
import com.ziyueyuwen.xiniao.bean.ZyUserRole;
import com.ziyueyuwen.xiniao.dao.ZyRoleMapper;
import com.ziyueyuwen.xiniao.dao.ZyUserRoleMapper;
import com.ziyueyuwen.xiniao.enums.RoleEnum;
import com.ziyueyuwen.xiniao.service.RoleService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wancheng  on 2018/12/23.
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    ZyRoleMapper zyRoleMapper;
    @Autowired
    ZyUserRoleMapper zyUserRoleMapper;

    @Override
    public List<ZyRole> getAllRole() {
        return zyRoleMapper.selectAll();
    }

    @Override
    public List<ZyRole> getUerRolesByAccount(String userAccount) {
        return zyRoleMapper.getUserRole(userAccount);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ZyRole> updateUserRoles(String userAccount, List<Integer> roleIds) {
        List<ZyRole> roles = zyRoleMapper.getUserRole(userAccount);
        List<Integer> existIds = roles.stream().map(ZyRole::getId).collect(Collectors.toList());
        List<Integer> needInsert = roleIds.stream().filter(id->!existIds.contains(id)).collect(Collectors.toList());
        List<Integer> needDelete = existIds.stream().filter(id->!roleIds.contains(id)).collect(Collectors.toList());
        deleteRoles(userAccount,needDelete);
        insertRoles(userAccount,needInsert);
        List<ZyRole> res = zyRoleMapper.getUserRole(userAccount);
        return res;
    }

    @Override
    public void deleteRoles(String userAccount, List<Integer> roleIds) {
        roleIds.forEach(
                id->{
                    ZyUserRole userRole = new ZyUserRole();
                    userRole.setZyUserAccount(userAccount);
                    userRole.setZyRoleId(id);
                    zyUserRoleMapper.delete(userRole);
                }
        );
    }

    @Override
    public void insertRoles(String userAccount, List<Integer> roleIds) {
        roleIds.forEach(
                id->{
                    ZyUserRole userRole = new ZyUserRole();
                    userRole.setZyUserAccount(userAccount);
                    userRole.setZyRoleId(id);
                    zyUserRoleMapper.insertSelective(userRole);
                }
        );
    }

    @Override
    public Boolean ifHasRole(String userAccount, RoleEnum roleEnum) {
        ZyRole role = new ZyRole();
        role.setRole(roleEnum);
        role = zyRoleMapper.selectOne(role);
        if (role == null){
            return false;
        }
        ZyUserRole userRole = new ZyUserRole();
        userRole.setZyRoleId(role.getId());
        userRole.setZyUserAccount(userAccount);
        List<ZyUserRole> userRoles = zyUserRoleMapper.select(userRole);
        if (CollectionUtils.isNotEmpty(userRoles)){
            return true;
        }
        return false;
    }
}
